unit untCadRegionais;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, untAbsConsulta, Vcl.ActnList, Vcl.Menus,
  Vcl.ImgList, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.ComCtrls,
  Vcl.ToolWin, Data.DB, Data.Win.ADODB;

type
  TfrmCadRegionais = class(TfrmAbsConsulta)
    qryRegionais: TADOQuery;
    dtsRegionais: TDataSource;
    qryRegionaisID: TAutoIncField;
    qryRegionaisNOME_RAZAO: TStringField;
    qryRegionaisNOME_FANTASIA: TStringField;
    qryRegionaisCIDADE: TStringField;
    qryRegionaisUF: TStringField;
    qryRegionaisDT_CADASTRO: TDateTimeField;
    procedure qryRegionaisDT_CADASTROGetText(Sender: TField; var Text: string;
      DisplayText: Boolean);
    procedure dtsRegionaisStateChange(Sender: TObject);
    procedure edtPesquisaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure qryRegionaisAfterScroll(DataSet: TDataSet);
    procedure acAlterarExecute(Sender: TObject);
    procedure acIncluirExecute(Sender: TObject);
  private
    { Private declarations }
    procedure geraConsulta(const value : String);
  public
    { Public declarations }
  end;

var
  frmCadRegionais: TfrmCadRegionais;

implementation

{$R *.dfm}

uses untDTMPrincipal, untCadRegionaisEdicao;

procedure TfrmCadRegionais.acAlterarExecute(Sender: TObject);
var
  frm : TfrmCadRegionaisEdicao;
begin
  FInsert := False;
  try
    frm := TfrmCadRegionaisEdicao.Create(self, qryRegionaisID.AsInteger);
    frm.ShowModal();
    AtualizaCds();
  finally
    FreeAndNil(frm);
  end;
  inherited;
end;

procedure TfrmCadRegionais.acIncluirExecute(Sender: TObject);
var
  frm : TfrmCadRegionaisEdicao;
begin
  FInsert := True;
  try
    frm := TfrmCadRegionaisEdicao.Create(self);
    frm.ShowModal();
    AtualizaCds();
  finally
    FreeAndNil(frm);
  end;
  inherited;
end;

procedure TfrmCadRegionais.dtsRegionaisStateChange(Sender: TObject);
begin
  inherited;
  AtualizarBotoes();
end;

procedure TfrmCadRegionais.edtPesquisaKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if Key = 13 then
    geraConsulta(edtPesquisa.Text);
end;

procedure TfrmCadRegionais.geraConsulta(const value: String);
var
  comandoSQL : TStringList;
begin
  try
    try
      comandoSQL := TStringList.Create;

      comandoSQL.Add(' WHERE 1=1');

      if cbxFiltro.Text = 'Nome' then
         comandoSQL.Add(format(' AND NOME_RAZAO LIKE %s',[QuotedStr('%'+value+'%')]));

      if cbxFiltro.Text = 'Apelido' then
         comandoSQL.Add(format(' AND NOME_FANTASIA LIKE %s',[QuotedStr('%'+value+'%')]));

      if cbxFiltro.Text = 'Cidade' then
         comandoSQL.Add(format(' AND CIDADE LIKE %s',[QuotedStr('%'+value+'%')]));

      if cbxFiltro.Text = 'UF' then
         comandoSQL.Add(format(' AND UF = %s',[QuotedStr(value)]));

       cdsAtual.Close;
       cdsAtual.SQL.Clear;
       cdsAtual.SQL.Add(FSQL+comandoSQL.Text);
       cdsAtual.Open();
     except
       cdsAtual.Close();
       cdsAtual.SQL.Clear();
       cdsAtual.SQL.Text:= FSQL;
       cdsAtual.Open();
       raise;
     end;
  finally
    FreeAndNil(comandoSQL);
  end;
end;

procedure TfrmCadRegionais.qryRegionaisAfterScroll(DataSet: TDataSet);
begin
  inherited;
  AtualizarBotoes();
end;

procedure TfrmCadRegionais.qryRegionaisDT_CADASTROGetText(Sender: TField;
  var Text: string; DisplayText: Boolean);
begin
  Text := FormatDateTime('DD/MM/YYYY',Sender.AsDateTime);
end;

end.
